package com.ruoyi.web.controller.blogs;

import com.ruoyi.blogs.domain.BsResource;
import com.ruoyi.blogs.service.IBsResourceService;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.utils.uuid.IdUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 资源Controller
 *
 * @author ruoyi
 * @date 2022-10-28
 */
@RestController
@RequestMapping("/blogs/resource")
public class BsResourceController extends BaseController {
    @Autowired
    private IBsResourceService bsResourceService;

    /**
     * 查询资源列表
     */
    @PreAuthorize("@ss.hasPermi('blogs:resource:list')")
    @GetMapping("/list")
    public TableDataInfo list(BsResource bsResource) {
        startPage();
        if (!SecurityUtils.isAdmin(SecurityUtils.getUserId())) {
            bsResource.setUserId(SecurityUtils.getUserId());
        }
        List<BsResource> list = bsResourceService.selectBsResourceList(bsResource);
        return getDataTable(list);
    }

    /**
     * 导出资源列表
     */
    @PreAuthorize("@ss.hasPermi('blogs:resource:export')")
    @Log(title = "资源", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, BsResource bsResource) {
        List<BsResource> list = bsResourceService.selectBsResourceList(bsResource);
        ExcelUtil<BsResource> util = new ExcelUtil<BsResource>(BsResource.class);
        util.exportExcel(response, list, "资源数据");
    }

    /**
     * 获取资源详细信息
     */
    @PreAuthorize("@ss.hasPermi('blogs:resource:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") String id) {
        return AjaxResult.success(bsResourceService.selectBsResourceById(id));
    }

    /**
     * 新增资源
     */
    @PreAuthorize("@ss.hasPermi('blogs:resource:add')")
    @Log(title = "资源", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody BsResource bsResource) {
        bsResource.setId(IdUtils.fastSimpleUUID());
        return toAjax(bsResourceService.insertBsResource(bsResource));
    }

    /**
     * 修改资源
     */
    @PreAuthorize("@ss.hasPermi('blogs:resource:edit')")
    @Log(title = "资源", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody BsResource bsResource) {
        return toAjax(bsResourceService.updateBsResource(bsResource));
    }

    /**
     * 删除资源
     */
    @PreAuthorize("@ss.hasPermi('blogs:resource:remove')")
    @Log(title = "资源", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable String[] ids) {
        return toAjax(bsResourceService.deleteBsResourceByIds(ids));
    }
}
